﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Metro.Core;

namespace Today
{
    /// <summary>
    /// Interaction logic for WidgetControl.xaml
    /// </summary>
    public partial class WidgetControl : UserControl
    {
        private DispatcherTimer _timer;
        private Options _options;

        public WidgetControl()
        {
            InitializeComponent();

        }

        public void Load()
        {
            _timer = new DispatcherTimer { Interval = TimeSpan.FromMinutes(1) };
            _timer.Tick += TimerTick;
            _timer.Start();


            var optionsItem = new MenuItem { Header = Widget.Instance.LocaleManager.GetString("Options") };
            optionsItem.Click += OptionsItemClick;
            Widget.Instance.AddMenuItem(optionsItem);

            _options = new Options();

            Month.Text = DateTime.Now.ToString("MMMM");
            Day.Text = DateTime.Now.Day.ToString();

            UpdateSettings();
        }

        private void SetBg(int month)
        {
            switch (month)
            {
                case 12:
                case 1:
                case 2:
                    Widget.Instance.SetBackground(Resources["Winter"] as SolidColorBrush);
                    this.Foreground = Brushes.Black;
                    break;

                case 3:
                case 4:
                case 5:
                    Widget.Instance.SetBackground(Resources["Spring"] as SolidColorBrush);
                    this.Foreground = Brushes.White;
                    break;

                case 6:
                case 7:
                case 8:
                    Widget.Instance.SetBackground(Resources["Summer"] as SolidColorBrush);
                    this.Foreground = Brushes.White;
                    break;

                case 9:
                case 10:
                case 11:
                    Widget.Instance.SetBackground(Resources["Fall"] as SolidColorBrush);
                    this.Foreground = Brushes.White;
                    break;

                default:
                    Widget.Instance.SetBackground(null);
                    this.Foreground = Brushes.White;
                    break;
            }
        }

        void OptionsItemClick(object sender, RoutedEventArgs e)
        {
            if (_options.IsVisible)
            {
                _options.Activate();
                return;
            }
            _options = new Options();

            if (E.Locale == "he-IL" || E.Locale == "ar-SA")
            {
                _options.FlowDirection = FlowDirection.RightToLeft;
            }
            else
            {
                _options.FlowDirection = FlowDirection.LeftToRight;
            }

            _options.ShowDialog();
        }

        void TimerTick(object sender, EventArgs e)
        {
            Month.Text = DateTime.Now.ToString("MMMM");
            Day.Text = DateTime.Now.Day.ToString();
        }

        public void UpdateSettings()
        {
            if (Widget.Instance.Sett.ChangeBackgroundWithSeason)
                SetBg(DateTime.Now.Month);
            else
                SetBg(0);
        }
    }
}
